<?php
/*
 * $Id: PaginatorTest.class.php 146 2009-12-16 15:54:37Z namba0219 $
 * =============================================================================
 * Barge - web application framework on PHP5 
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * =============================================================================
 * For more information, see http://barge.googlecode.com/
 */
namespace barge\helper;

use \UnitTestCase;

use barge\helper\Paginator;

use barge\utils\ArrayList;
use barge\utils\Date;
use barge\utils\HashMap;
use barge\utils\Month;
use barge\utils\IndexOutOfBoundsException;
use barge\utils\IllegalAccessException;

/**
 * @see UnitTestCase
 */
class PaginatorTest extends UnitTestCase 
{
	/**
	 * @test
	 */
	public function testOffseted() 
	{
		//
		// 件数:1
		//
		$paginator = Paginator::wrapOffseted($this->createItems(1), 1, 20);
		$this->assertTrue($paginator->getCurrentPage()->getNo() === 1);
		$this->assertTrue($paginator->getCurrentPage()->getStartOffset() === 1);
		$this->assertTrue($paginator->getCurrentPage()->getEndOffset() === 1);
		$this->assertTrue($paginator->getMaxRows() === 20);
		$this->assertTrue($paginator->getTotalCount() === 1);
		$this->assertTrue($paginator->getTotalPage() === 1);
		$this->assertTrue($paginator->getHasNext() === false);
		$this->assertTrue($paginator->getHasPrev() === false);
		
		//
		// 件数:19
		//
		$paginator = Paginator::wrapOffseted($this->createItems(19), 1, 20);
		$this->assertTrue($paginator->getCurrentPage()->getNo() === 1);
		$this->assertTrue($paginator->getCurrentPage()->getStartOffset() === 1);
		$this->assertTrue($paginator->getCurrentPage()->getEndOffset() === 19);
		$this->assertTrue($paginator->getMaxRows() === 20);
		$this->assertTrue($paginator->getTotalCount() === 19);
		$this->assertTrue($paginator->getTotalPage() === 1);
		$this->assertTrue($paginator->getHasNext() === false);
		$this->assertTrue($paginator->getHasPrev() === false);
		try { 
			$paginator->pageNext();
			$this->fail(); 
		} catch (Exception $e) {}
		
		//
		// 件数:20
		//
		$paginator = Paginator::wrapOffseted($this->createItems(20), 1, 20);
		$this->assertTrue($paginator->getCurrentPage()->getNo() === 1);
		$this->assertTrue($paginator->getCurrentPage()->getStartOffset() === 1);
		$this->assertTrue($paginator->getCurrentPage()->getEndOffset() === 20);
		$this->assertTrue($paginator->getMaxRows() === 20);
		$this->assertTrue($paginator->getTotalCount() === 20);
		$this->assertTrue($paginator->getTotalPage() === 1);
		$this->assertTrue($paginator->getHasNext() === false);
		$this->assertTrue($paginator->getHasPrev() === false);
		try { 
			$paginator->pageNext();
			$this->fail(); 
		} catch (Exception $e) {}
		
		//
		// 件数:21
		//
		$paginator = Paginator::wrapOffseted($this->createItems(21), 1, 20);
		$this->assertTrue($paginator->getCurrentPage()->getNo() === 1);
		$this->assertTrue($paginator->getCurrentPage()->getStartOffset() === 1);
		$this->assertTrue($paginator->getCurrentPage()->getEndOffset() === 20);
		$this->assertTrue($paginator->getMaxRows() === 20);
		$this->assertTrue($paginator->getTotalCount() === 21);
		$this->assertTrue($paginator->getTotalPage() === 2);
		$this->assertTrue($paginator->getHasNext() === true);
		$this->assertTrue($paginator->getHasPrev() === false);
		// 1 -> 2
		$paginator->pageNext();
		$this->assertTrue($paginator->getCurrentPage()->getNo() === 2);
		$this->assertTrue($paginator->getCurrentPage()->getStartOffset() === 21);
		$this->assertTrue($paginator->getCurrentPage()->getEndOffset() === 21);
		$this->assertTrue($paginator->getHasNext() === false);
		$this->assertTrue($paginator->getHasPrev() === true);
		// 2 -> 1
		$paginator->pagePrev();
		$this->assertTrue($paginator->getCurrentPage()->getNo() === 1);
		$this->assertTrue($paginator->getCurrentPage()->getStartOffset() === 1);
		$this->assertTrue($paginator->getCurrentPage()->getEndOffset() === 20);
		$this->assertTrue($paginator->getHasNext() === true);
		$this->assertTrue($paginator->getHasPrev() === false);
	}

	/**
	 * @test
	 */
	public function createItems($len) 
	{
		$list = new ArrayList();
		for ($i = 1; $i <= $len; $i++) {
			$map = new HashMap();
			$map->put('id', $i);
			$list->add($map);
		}
		return $list;
	}
	
}
